package com.bobo.web.provider.util.commutils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.util.*;


/**
 * @author zouqinghua
 * @date 2016年5月4日  上午11:55:45
 *
 */
public class MapUtils {

	private static Logger logger = LoggerFactory.getLogger(MapUtils.class);

	public static boolean isNotEmpty(Map map) {
		return !isEmpty(map);
	}

	public static boolean isEmpty(Map map) {
		boolean empty = true;
		if(null != map && map.size() > 0 ) {
			empty = false;
		}
		return empty;
	}

	/**
	 * java bean 转换为Map ，默认过滤掉值为空的属性
	 * @param obj
	 * 简单bean转换为Map
	 * @return
	 */
	public static Map<String, Object> transBeanToMap(Object obj){
		return transBeanToMap(obj,null,null);
	}

	/**
	 * java bean 转换为Map ，默认过滤掉值为空的属性
	 * @param obj
	 * @param isExceptNull
	 * @return
	 */
	public static Map<String, Object> transBeanToMap(Object obj,Boolean isExceptNull){
		return transBeanToMap(obj, null, isExceptNull);
	}

	/**
	 * java bean 转换为Map ，默认过滤掉值为空的属性
	 * @param obj
	 * @param exceptAttrs
	 * @return
	 */
	public static Map<String, Object> transBeanToMap(Object obj,List<String> exceptAttrs){
		return transBeanToMap(obj, exceptAttrs, null);
	}

	/**
	 * java bean 转换为Map ，默认过滤掉值为空的属性
	 * @param obj
	 * @param exceptAttrs
	 * @param isExceptNull
	 * @return
	 */
	public static Map<String, Object> transBeanToMap(Object obj,List<String> exceptAttrs,Boolean isExceptNull){
		Map<String, Object> map = null;

		if(isExceptNull == null){
			isExceptNull = true;
		}
		if(exceptAttrs == null){
			exceptAttrs = Lists.newArrayList();
		}
		if(obj!=null){
			map = new HashMap<String, Object>();

			try {
				BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
				PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
				for (PropertyDescriptor property : propertyDescriptors) {
					String key = property.getName();
					if(StringUtils.isBlank(key) || key.equals("class")){
						continue; // 过滤class属性
					}
					if(exceptAttrs.contains(key)){
						continue; //过滤属性
					}
					// 得到property对应的getter方法
					Method getter = property.getReadMethod();
					if(getter!=null){
						Object value = getter.invoke(obj);
						if(isExceptNull == true && isFilterByValue(value) ){
							continue;
						}else if(value instanceof Timestamp ){
							value = ((Timestamp)value).getTime();
						}else if(value instanceof Date){
							value = DateTimeUtils.format((Date)value , "");
						}
						map.put(key, value);
					}
				}
			} catch (Exception e) {
				logger.error("transBean2Map Error e={}",e);
			}
		}
		return map;
	}

	/**
	 * 过滤掉值为空的属性
	 * @param value
	 * @return
	 */
	public static Boolean isFilterByValue(Object value) {
		Boolean flag = false;
		if (value == null || StringUtils.isBlank(value.toString())) {
			flag = true;
		}
		if (value instanceof Map && ((Map) value).isEmpty()) {
			flag = true;
		}
		if (value instanceof List && ((List) value).isEmpty()) {
			flag = true;
		}
		return flag;
	}

	public static Map<String,String> ObjectToMapStringValue(Object obj){
		Map<String, Object> mapObj = transBeanToMap(obj);
		Map<String,String> map = Maps.newHashMap();
		for (String key : mapObj.keySet()) {
			String value = mapObj.get(key)==null?null:mapObj.get(key).toString();
			map.put(key, value );
		}
		return map;
	}

	public static Map<String, Object> parseJsonToMap(JSONObject jsonObject){
		return parseJsonToMap(jsonObject, null);
	}

	public static Map<String, Object> parseJsonToMap(JSONObject jsonObject, String keyPre){
		Map<String, Object> map = Maps.newHashMap();
		if(StringUtils.isNoneBlank(keyPre)){
			keyPre += ".";
		}else{
			keyPre ="";
		}
		if(jsonObject!=null && !jsonObject.isEmpty()){
			Set<String> keys = jsonObject.keySet();
			for (String key : keys) {
				Object object = jsonObject.get(key);
				key = keyPre+key;
				if(object instanceof String || object instanceof Integer){
					map.put(key,object);
				}else if(object instanceof JSONArray){
					map.putAll(parseJsonToMap((JSONArray)object,key));
				}else if(object instanceof JSONObject){
					map.putAll(parseJsonToMap((JSONObject)object,key));
				}else{
					map.put(key,object);
				}

			}
		}

		return map;
	}

	public static Map<String, Object> parseJsonToMap(JSONArray jsonArray, String keyPre){
		Map<String, Object> map = Maps.newHashMap();
		if(keyPre == null){
			keyPre ="";
		}
		if(jsonArray!=null && !jsonArray.isEmpty()){
			Integer index = 0;
			for (Object object : jsonArray) {
				String key = keyPre+"["+index+"]";
				if(object instanceof String || object instanceof Integer){
					map.put(key,object);
				}if(object instanceof JSONArray){
					map.putAll(parseJsonToMap((JSONArray)object,key));
				}else if(object instanceof JSONObject){
					map.putAll(parseJsonToMap((JSONObject)object,key));
				}else{
					map.put(key,object);
				}
				index++;
			}
		}
		return map;
	}
}
